# https://leetcode-cn.com/problems/unique-paths-ii/
# 63. 不同路径 II

from typing import List


class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        if (obstacleGrid == [[0]]):
            return 1
        if (obstacleGrid[0][0]):
            return 0
        m = len(obstacleGrid)
        n = len(obstacleGrid[0])
        # print(m,n)
        dp = [[0 for i in range(n)] for j in range(m)]
        # print(dp)
        if (m >= 2):
            for i in range(1, m):
                if (obstacleGrid[i][0] == 0):
                    dp[i][0] = 1
                else:
                    break
        # print(dp)
        if (n >= 2):
            for i in range(1, n):
                if (obstacleGrid[0][i] == 0):
                    dp[0][i] = 1
                else:
                    break
        # print(dp)

        for i in range(1,m):
            for j in range(1,n):
                if (obstacleGrid[i][j]):
                    dp[i][j] = 0
                    continue
                else:
                    dp[i][j] = dp[i-1][j] + dp[i][j-1]
            
        # print(dp)
        return dp[m-1][n-1]
                    
                


a = Solution()
a.uniquePathsWithObstacles([[0,0]])